<?php

class FP_Image
{
    const PNG = 'png';
    const JPEG = 'jpg';
    const GIF = 'gif';

    protected $image = null;
    protected $width = null;
    protected $height = null;
    protected $path = null;

    public function __construct($pFile){
	$this->create($pFile);
    }

    protected function create($pFile, $pType=null){

	if (is_resource($pFile)){ //gd resource
	    $image = $pFile;
	} else if (is_object($pFile) && $pFile instanceof FP_Image){ //FP_Image object
	    $image = $pFile->image;
	} else {  //filepath/url

	if (preg_match('/^\w+:\/\/.*/', $pFile)){                 //Image path is url
	    $meta = stream_get_meta_data(fopen($pFile, 'r'));

	    foreach ($meta['wrapper_data'] as $header){
		if (preg_match('/^Location:\ /', $header)){            //Is this a redirect? Get real url
		    $redirect = true;
		    $url = preg_replace('/^Location:\ /', '', $header);
		}
	    }

	    if (!$redirect)
		$url = $pFile;

	    $url_path = parse_url($url, PHP_URL_PATH);
	    $path_parts = pathinfo($url_path);
	    $ext = $path_parts['extension'];
	} else {                                                   //Just a file
	    $path_parts = pathinfo($pFile);
	    $ext = $path_parts['extension'];
	    $url = $pFile;
	    $this->path = $pFile;
	}



	if ($pType)
	    $ext = $pType;

	switch(strtolower($ext)){
	    case 'jpg':
	    case 'jpeg':
		$image = imagecreatefromjpeg($url);
		break;
	    case 'png':
		$image = imagecreatefrompng($url);
		break;
	    case 'gif':
		$image = imagecreatefromgif($url);
		break;
	    default:
		throw new FP_Image_Exception("Unsupported format $ext ($url)", FP_Image_Exception::UNSUPPORTED_FORMAT);
	    }
	}

	$this->width = imageSX($image);
	$this->height= imageSY($image);

	logdebug("FP_Image.create: Processing $url width={$this->width} height={$this->height}");

	$this->image =& $image;
	return $image;
    }

    public function validateCacheDir($pCacheDir){
	if (file_exists($pCacheDir) && is_dir($pCacheDir)){
	    return true;
	} else {
	    return mkdir($pCacheDir);
	}
    }

    public function __get($pAtt){
	switch (strtolower($pAtt)){
	case 'width':
	return $this->width;
	case 'height':
	return $this->height;
	case 'image':
	return $this->image;
	case 'path':
	    return $this->path;
	}
    }

    public function save($pDestination, $pQuality=null){
	$path_parts = pathinfo($pDestination);
	$ext = strtolower($path_parts['extension']);

	$this->path = $pDestination;

	$path_parts = pathinfo($pDestination);
	$dir = $path_parts['dirname'];
	if (!file_exists($dir))
	    mkdir($dir, 0775, true);

	switch (strtolower($ext)){
	    case 'jpg':
	    case 'jpeg':
		if (is_null($pQuality))
		    $quality = 100;
		else
		    $quality = $pQuality;

		return imagejpeg($this->image,$pDestination,$quality);
	    case 'png':
		if (is_null($pQuality))
		    $quality = 9;
		else
		    $quality = $pQuality;

		return imagepng($this->image,$pDestination,$quality);
	    case 'gif':
		return imagegif($this->image,$pDestination);
	    default:
		throw new FP_Image_Exception("Unsupported format $ext", FP_Image_Exception::UNSUPPORTED_FORMAT);

	}
    }

    public function stream($pFormat=self::PNG, $pQuality=null){
	switch ($pFormat){
	case self::PNG:
	if (is_null($pQuality))
	    $quality = 9;
	else
	    $quality = $pQuality;
	imagepng($this->image, null, $quality);
	break;
	case self::JPEG:
	if (is_null($pQuality))
	    $quality = 100;
	else
	    $quality = $pQuality;

	imagejpeg($this->image, null, $quality);
	break;
	case self::GIF:
	imagegif($this->image);
	break;
	default:
	throw new FP_Image_Exception("Unsupported format $pFormat", FP_Image_Exception::UNSUPPORTED_FORMAT);
	}
    }

    public function __destroy(){
	imagedestroy($this->image);
    }
}

?>